package com.zeng.plugin.intefaceHandle.impl;

import com.zeng.plugin.entity.TableEntity;
import com.zeng.plugin.intefaceHandle.ParseHandle;
import com.zeng.plugin.util.FreeMarkUtil;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class WrapperParseHandle implements ParseHandle {
    private Map<String, Object> data;

    public WrapperParseHandle(Map<String, Object> data) {
        this.data = data;
    }

    public WrapperParseHandle() {

    }
    @Override
    public void parse(List<TableEntity> tables, Map<String, String> mapPackage, String javaPath, String templateFile, FreeMarkUtil freeMarkUtil) {
        for (TableEntity table : tables) {
            Map<String, Object> data = new HashMap<>();
            String className = table.getClassName() + "Wrapper";
            String voClassName = table.getClassName() + "Vo";
            String entityClassName = table.getClassName();
            for (String s : mapPackage.keySet()) {
                data.put(s, mapPackage.get(s));
            }
            data.put(className, className);
            data.put("entityClassName", entityClassName);
            data.put("voClassName", voClassName);
            data.put("table", table);
            if (this.data != null) {
                Map<String, List<String>> stringListMap = (Map<String, List<String>>) this.data.get("like");
                data.put("like", stringListMap.get(table.getRealTableName()));
            }
            try {
                File file = new File(javaPath + "\\" + className + ".java");
                if (!file.exists()) {
                    freeMarkUtil.parse(templateFile, file, data);
                }
            } catch (Exception e) {
                System.out.println(e.getMessage());

            }
        }
    }
}
